/*
 * @(#)Utils.java Jan 11, 2012 - 7:40:37 PM
 * 
 * Copyright 2011 Cin/UFPE/iSPEED
 */
package speed2010.reformulacao.useful;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import speed2010.util.SpeedLocalOntology;

/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class Utils {
	/**
	 * Recovery the header element from a ontology
	 * 
	 * @param type
	 *            - The element type
	 * @param stream
	 *            - A {@link InputStream} from the ontology
	 * @return
	 */
	public static String getHeaderElement(String type, InputStream stream) {
		String element = "";
		String line = null;

		BufferedReader br = new BufferedReader(new InputStreamReader(stream));
		try {
			while ((line = br.readLine()) != null) {
				if (line.contains(type)) {
					element = line.substring(line.indexOf('"') + 1, line.lastIndexOf('"'));
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return element;
	}

	/**
	 * Recovery the header element from a ontology
	 * 
	 * @param type
	 *            - The element type
	 * @param ontology
	 *            - The {@link SpeedLocalOntology} instance
	 * 
	 * @return
	 */
	public static String getHeaderElement(String type, SpeedLocalOntology ontology) {
		ByteArrayInputStream bais = new ByteArrayInputStream(ontology.ontologia.getBytes());

		return getHeaderElement(type, bais);
	}

	/**
	 * Recovery the header element from a ontology
	 * 
	 * @param type
	 *            - The element type
	 * @param ontologyPath
	 *            - The file system path for the ontology
	 * @return
	 */
	public static String getHeaderElement(String type, String ontologyPath) {
		String element = "";
		try {
			FileInputStream fis = new FileInputStream(new File(ontologyPath));
			element = getHeaderElement(type, fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return element;
	}

	/**
	 * Recovery the header element from a ontology
	 * 
	 * @param type
	 *            - The element type
	 * @param ontology
	 *            - The file system path for the ontology byte array
	 * @return
	 */
	public static String getHeaderElement(String type, byte[] ontology) {
		ByteArrayInputStream bais = new ByteArrayInputStream(ontology);

		return getHeaderElement(type, bais);
	}

	/**
	 * Convert an input stream to a byte array
	 * 
	 * @param stream
	 *            - The input stream
	 * @return {@code byte[]} The byte content of the stream
	 * @throws IOException
	 */
	public static byte[] streamToArray(final InputStream stream) throws IOException {
		byte[] byteArray;
		byteArray = new byte[stream.available()];
		stream.read(byteArray);

		if (byteArray.length <= 0) {
			throw new IOException("The file name " + " is empty.");
		}

		return byteArray;
	}

	/**
	 * Get a byte[] from a file
	 * 
	 * @param file
	 *            - The input file
	 * @return {@code byte[]} The byte content of the file
	 * 
	 * @throws IOException
	 */
	public static byte[] getBytesFromFile(final File file) throws IOException {
		FileInputStream fis = new FileInputStream(file);
		byte[] byteArray = streamToArray(fis);

		return byteArray;
	}

	/**
	 * Get a byte[] from a file in path
	 * 
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static byte[] getBytesFromFile(final String filePath) throws IOException {
		FileInputStream fis = new FileInputStream(new File(filePath));
		byte[] byteArray = streamToArray(fis);

		return byteArray;
	}

}
